//
//  AuthorizeLoginController.m
//  032519-友盟统计SDK
//
//  Created by 强进冬 on 2019/3/27.
//  Copyright © 2019 强进冬. All rights reserved.
//

#import "AuthorizeLoginController.h"
#import "UMTool.h"
#import "ShareController.h"

@interface AuthorizeLoginController () <UITableViewDataSource, UITableViewDelegate>
@property (weak, nonatomic) IBOutlet UITableView *tableView;

@property (nonatomic, strong) NSArray<NSString *> *dataSource;

@end

@implementation AuthorizeLoginController

#pragma mark - life cycle
- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"授权登录";
    self.tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
    [self.tableView registerClass:UITableViewCell.self forCellReuseIdentifier:@"AuthorizeLoginCell"];
}

#pragma mark - <UITableViewDataSource, UITableViewDelegate>
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataSource.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"AuthorizeLoginCell" forIndexPath:indexPath];
    cell.selectionStyle = UITableViewCellSeparatorStyleNone;
    cell.textLabel.text = [NSString stringWithFormat:@"%@", self.dataSource[indexPath.row]];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    UMSocialPlatformType platform = UMSocialPlatformType_UnKnown;
    switch (indexPath.row) {
        case 0:
            platform = UMSocialPlatformType_Sina;
            break;
        case 1:
            platform = UMSocialPlatformType_WechatSession;
            break;
        case 2:
            platform = UMSocialPlatformType_QQ;
        default:
            break;
    }
    [self getUserInfoWithPlatform:platform];
}

#pragma mark - lazy
- (NSArray<NSString *> *)dataSource {
    if (!_dataSource) {
        _dataSource = @[@"新浪微博", @"微信", @"QQ"];
    }
    return _dataSource;
}

#pragma mark - private methods
- (void)getUserInfoWithPlatform:(UMSocialPlatformType)platformType {
    [UMTool getUserInfoWithPlatform:platformType currentViewController:self completion:^(id result, NSError *error) {
        if (error) {
            NSLog(@"error: %@", error);
            return;
        }
        UMSocialUserInfoResponse *resp = result;
        
        NSLog(@"%@", resp);
        
        // 授权信息
        NSLog(@" uid: %@", resp.uid);
        NSLog(@" openid: %@", resp.openid);
        NSLog(@" unionid: %@", resp.unionId);
        NSLog(@" accessToken: %@", resp.accessToken);
        NSLog(@" refreshToken: %@", resp.refreshToken);
        NSLog(@" expiration: %@", resp.expiration);
        // 用户信息
        NSLog(@" name: %@", resp.name);
        NSLog(@" iconurl: %@", resp.iconurl);
        NSLog(@" gender: %@", resp.unionGender);
        // 第三方平台SDK源数据
        NSLog(@" originalResponse: %@", resp.originalResponse);
    }];
}

@end
